using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Windows;

[assembly: AssemblyTitle("Convert XAML to EMF")]
[assembly: AssemblyProduct("XamlToys")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyCompany("BCDev")]
[assembly: AssemblyCopyright("© 2007 Blake Coverett")]
[assembly: AssemblyDescription("Distribute freely under the Ms-PL.")]

namespace BCDev.XamlToys {
	class Xaml2Emf {
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        [STAThread]
		static int Main(string[] args) {
			if (args.Length != 2) Fail("Usage: xaml2emf <xaml-input-file> <emf-output-file>");

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
            try {
				var drawing = Utility.GetDrawingFromXaml(Utility.LoadXamlFromFile(args[0]));
				if (drawing == null) Fail("Error: Unable to create Drawing from Xaml");

				var bounds = drawing.Bounds;
				Console.WriteLine("Drawing Bounds: {0}", bounds);

				using (var g = Utility.CreateEmf(args[1], bounds))
					Utility.RenderDrawingToGraphics(drawing, g);

			} catch (Exception e) {
				Fail(e.Message);
			}

            DebugPause();
			return 0;
		}

        static void Fail(string message) {
            Console.Error.WriteLine(message);
            DebugPause();
            Environment.Exit(1);
        }

        [Conditional("DEBUG")]
        static void DebugPause() {
            Console.Write("\r\nPress any key to exit...");
            Console.ReadKey();
        }
	}
}
